﻿using System;
using System.ComponentModel;
using System.Data.EntityClient;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Runtime.Serialization;
using System.Xml.Serialization;

namespace EQEmu
{
    public partial class doors : EntityObject
    {
        //http://www.eqemulator.net/wiki/wikka.php?wakka=OpenType
        public enum OpenTypes
        {
            Normal90DegreeDoorSwingBackward_0 = 0,
            Normal90DegreeDoorSwingBackward_1 = 1,
            Normal90DegreeDoorSwingBackward_2 = 2,
            Normal90DegreeDoorSwingBackward_3 = 3,
            Normal90DegreeDoorSwingBackward_4 = 4,

            Normal90DegreeDoorSwingForward_0 = 5,
            Normal90DegreeDoorSwingForward_1 = 6,
            Normal90DegreeDoorSwingForward_2 = 7,

            Normal90DegreeDoorSwingBackward_5 = 8,

            SlidesForward_0 = 10,
            SlidesForward_1 = 11,
            SlidesForward_2 = 12,

            SlidesFurtherForward_0 = 15,
            SlidesFurtherForward_1 = 16,
            SlidesFurtherForward_2 = 17,

            SlidesEvenFurtherForward_0 = 20,
            SlidesEvenFurtherForward_1 = 21,
            SlidesEvenFurtherForward_2 = 22,

            SlidesFurthestFoward_0 = 25,
            SlidesFurthestFoward_1 = 26,
            SlidesFurthestFoward_2 = 27,

            Rotates90DegreesClockwiseAndReturns = 30,

            Rotates90DegreesClockwiseAndReturnsFaster = 35,

            Rotates90DegreesAndJumpsBack = 36,

            Rotates90DegreesClockwiseAndReturnsSlower = 40,

            SlideSidewaysOpenAndClosesSlowly = 45,

            NoDoorShowingInvisible_0 = 50,
            NoDoorShowingInvisible_1 = 53,
            NoDoorShowingInvisible_2 = 54,

            ClickablePortal = 58,

            Unknown59 = 59,

            VerticalLiftLow_0 = 60,
            VerticalLiftLow_1 = 61,
            VerticalLiftLow_2 = 62,

            VerticalLiftExtremelyHigh_0 = 63,
            VerticalLiftExtremelyHigh_1 = 64,

            VerticalLiftLowMidHeight_0 = 65,
            VerticalLiftLowMidHeight_1 = 66,
            VerticalLiftLowMidHeight_2 = 67,

            VerticalLiftExtremelyHigh_2 = 68,
            VerticalLiftExtremelyHigh_3 = 69,

            VerticalLiftMidHeight_0 = 70,
            VerticalLiftMidHeight_1 = 71,
            VerticalLiftMidHeight_2 = 72,

            VerticalLiftExtremelyHigh_4 = 73,
            VerticalLiftExtremelyHigh_5 = 74,

            VerticalLiftHighHeight_0 = 75,
            VerticalLiftHighHeight_1 = 76,
            VerticalLiftHighHeight_2 = 77,

            MovesSlightlyForward_0 = 78,

            MovesSlightlyBackward_0 = 79,

            MovesSlightlyForward_1 = 80,

            MovesSlightlyBackwards_1 = 81,

            RevolvingDoorCounterClockwise = 100,

            RevolvingDoorCounterClockwiseFast = 101,

            RevolvingDoorCounterClockwiseFaster = 102,

            ContinousCounterClockwiseRotation = 106,

            ContinousCounterClockwiseRotationFaster = 107,

            SlideSidewaysOpenAndCloseImmediately = 109,

            //The following all need invert_state to 1 to work correctly. If invert_state is 0, they don't move but you take damage still if you touch them (Thanks Qadar, for this information):

            SpinNonstop4SawDamage = 115,
            SpinPause4SawDamage = 116,
            MovesDownThenUp30SpearDamage = 120,
            MovesLeftThenRight30SpearDamage = 125,
            SwingsBackAndForth4PendulumDamage = 130,
            NoMovement4BladeDamage = 135,
            MovesUpAndDown4CrushDamage = 140,
            MovesUpThenDownNonstop_0 = 142,
            MovesUpThenDownNonstop_1 = 143,
            MovesUpThenDownNonstop_2 = 144,
            DownThenUpSlow10CrushDamage = 145,
            DownThenUpFast50CrushDamage = 146,
            SlideFastLeftBackSlowly10CrushDamage = 150,
            SlideSlowLeftBackSlowly10CrushDamage = 151,
            SlideFastLeftBackQuickly50CrushDamage = 152
        }

        [XmlIgnore]
        public OpenTypes OpenTypeEnum
        {
            get
            {
                return (OpenTypes)opentype;
            }
            set
            {
                this.opentype = (short)value;
            }
        }

        [XmlIgnore]
        public float HeadingDegrees
        {
            get
            {
                return this.heading / 512 * 360;                
            }
            set
            {
                this.heading = value / 360 * 512;
            }
        }

        public void LookAt(double x,double y,double z)
        {
            double a = x - this.pos_x;
            double b = y - this.pos_y;
            double degrees = Math.Atan(b / a) * 180 / Math.PI;

            if (a == 0)
            {
                if (b > 0) degrees = 45.0;
                else if (b < 0) degrees = 270.0;
                else degrees = 0.0;
            }
            else if (b == 0)
            {
                if (a > 0) degrees = 0.0;
                else if (a < 0) degrees = 180.0;
                else degrees = 0.0;
            }
            else
            {
                if (a > 0)
                {
                    if (b > 0)
                    {
                        //quadrant 1                   
                        degrees = (90 - degrees) % 360;
                    }
                    else if (b < 0)
                    {
                        //quadrant 4
                        degrees = (90 - degrees) % 360;
                    }
                }
                else
                {
                    if (b > 0)
                    {
                        //quadrant 2
                        degrees = (270 - degrees) % 360;
                    }
                    else
                    {
                        //quadrant 3
                        //degrees = (180 + degrees) % 360;
                        degrees = (270 - degrees) % 360;
                    }
                }
            }
            degrees += 90;
            this.HeadingDegrees = (float)degrees;
        }
    }
}
